package com.yy.finvideoplayer.model

import android.graphics.Color
import com.yy.finvideoplayer.utils.ColorUtil

/**
 * @Description: PlayerOption
 * @Author: yiw89
 * @CreateDate: 2025/8/19 16:01
 * @Version: 1.0
 */
data class PlayerOptions (
    var videoPlayerId: String,
    var position: Position?,
    // 基础播放配置
    var src: String = "",
    var duration: Long = 0,
    var autoplay: Boolean = false,
    var loop: Boolean = false,
    var muted: Boolean = false,
    var initialTime: Long = 0,
    var poster: String = "",
    var title: String = "",

    // 控制器显示配置
    var controls: Boolean = true,
    var showProgress: Boolean = true,
    var showFullscreenBtn: Boolean = true,
    var showPlayBtn: Boolean = true,
    var showCenterPlayBtn: Boolean = true,
    var showMuteBtn: Boolean = false,
    var showBottomProgress: Boolean = true,
    var showCastingButton: Boolean = false,
    var showScreenLockButton: Boolean = false,
    var showSnapshotButton: Boolean = false,
    var showBackgroundPlaybackButton: Boolean = true,

    // 弹幕配置
    var danmuList: List<DanmakuItem> = emptyList(),
    var danmuBtn: Boolean = false,
    var enableDanmu: Boolean = false,

    // 手势配置
    var enableProgressGesture: Boolean = true,
    var enablePlayGesture: Boolean = false,
    var vslideGesture: Boolean = false,
    var vslideGestureInFullscreen: Boolean = true,

    // 全屏和方向配置
    var direction: Int? = null, // 0:正常竖向, 90:逆时针90度, -90:顺时针90度   null：宽高比自动判断
    var enableAutoRotation: Boolean = false,

    // 播放按钮位置
    var playBtnPosition: String = PlayBtnPosition.BOTTOM.value,

    // 视频适配模式
    var objectFit: String = ObjectFit.CONTAIN.value,

    // 自动暂停配置
    var autoPauseIfNavigate: Boolean = true,
    var autoPauseIfOpenNative: Boolean = true,

    // 小窗模式配置
    var pictureInPictureMode: List<PictureInPictureMode> = emptyList(),
    var pictureInPictureShowProgress: Boolean = false,
    var pictureInPictureInitPosition: String = "",

    // 后台播放配置
    var backgroundPoster: String = "",

    // 解码器配置
    var decoderType: DecoderType = DecoderType.MEDIA_PLAYER
){

    data class DanmakuItem(var text:String = "",
                           var time:Long = 0L,
                           var color:String = ""){
        val timeInMills get() = time * 1000
        val colorValue
            get() = try {
                ColorUtil.parseColor(color)
            } catch (e: Throwable) {
                Color.WHITE
            }
    }

    enum class DecoderType {
        MEDIA_PLAYER, IJK_PLAYER, EXO_PLAYER
    }

    enum class PlayBtnPosition(val value: String) {
        BOTTOM("bottom"), CENTER("center")
    }

    enum class ObjectFit(val value: String) {
        CONTAIN("contain"), FILL("fill"), COVER("cover")
    }

    enum class PictureInPictureMode {
        PUSH, POP
    }

    // 更新方法，只更新非null的属性
    fun updateWith(updates: Map<String, Any?>) {
        updates.forEach { (key, value) ->
            when (key) {
                "src" -> value?.let { src = it as String }
                "duration" -> value?.let { duration = (it as Double).toLong()}
                "autoplay" -> value?.let { autoplay = it as Boolean }
                "loop" -> value?.let { loop = it as Boolean }
                "muted" -> value?.let { muted = it as Boolean }
                "initialTime" -> value?.let {
                    initialTime = (it as Double).toLong()
                }
                "poster" -> value?.let { poster = it as String }
                "title" -> value?.let { title = it as String }
                "controls" -> value?.let { controls = it as Boolean }
                "showProgress" -> value?.let { showProgress = it as Boolean }
                "showFullscreenBtn" -> value?.let { showFullscreenBtn = it as Boolean }
                "showPlayBtn" -> value?.let { showPlayBtn = it as Boolean }
                "showCenterPlayBtn" -> value?.let { showCenterPlayBtn = it as Boolean }
                "showMuteBtn" -> value?.let { showMuteBtn = it as Boolean }
                "showBottomProgress" -> value?.let { showBottomProgress = it as Boolean }
                "showCastingButton" -> value?.let { showCastingButton = it as Boolean }
                "showScreenLockButton" -> value?.let { showScreenLockButton = it as Boolean }
                "showSnapshotButton" -> value?.let { showSnapshotButton = it as Boolean }
                "showBackgroundPlaybackButton" -> value?.let { showBackgroundPlaybackButton = it as Boolean }
                "danmuBtn" -> value?.let { danmuBtn = it as Boolean }
                "enableDanmu" -> value?.let { enableDanmu = it as Boolean }
                "enableProgressGesture" -> value?.let { enableProgressGesture = it as Boolean }
                "enablePlayGesture" -> value?.let { enablePlayGesture = it as Boolean }
                "vslideGesture" -> value?.let { vslideGesture = it as Boolean }
                "vslideGestureInFullscreen" -> value?.let { vslideGestureInFullscreen = it as Boolean }
                "direction" -> value?.let{ direction as Int }
                "enableAutoRotation" -> value?.let { enableAutoRotation = it as Boolean }
                "autoPauseIfNavigate" -> value?.let { autoPauseIfNavigate = it as Boolean }
                "autoPauseIfOpenNative" -> value?.let { autoPauseIfOpenNative = it as Boolean }
                "pictureInPictureShowProgress" -> value?.let { pictureInPictureShowProgress = it as Boolean }
                "pictureInPictureInitPosition" -> value?.let { pictureInPictureInitPosition = it as String }
                "backgroundPoster" -> value?.let { backgroundPoster = it as String }
                "decoderType" -> value?.let {
                    decoderType = DecoderType.valueOf((it as String).uppercase())
                }
                // 处理复杂类型
                "danmuList" -> value?.let {
                    @Suppress("UNCHECKED_CAST")
                    danmuList = it as List<DanmakuItem>
                }
                "playBtnPosition" -> value?.let {
                    playBtnPosition = it as String
                }
                "objectFit" -> value?.let {
                    objectFit = it as String
                }
                "pictureInPictureMode" -> value?.let {
                    @Suppress("UNCHECKED_CAST")
                    val modes = it as List<String>
                    pictureInPictureMode = modes.map { mode ->
                        PictureInPictureMode.valueOf(mode.uppercase())
                    }
                }
            }
        }
    }
}